package com.pda.yuanjing.pages.pro.scanf;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.ScanfBeanOut;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.pda.yuanjing.thread.CodeBeans;
import com.pda.yuanjing.tools.StringUtils;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.dialogs.ChooseDialog;
import com.public_module.tools.VoiceUtils;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProScanfActivity extends BaseActivity<ProScanfModel, ProScanfView, ProScanfPresent> implements ProScanfView {
    private String TAG = "ProScanfActivity";

    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.tvbill)
    TextView tvbill;
    @BindView(R.id.tvname)
    TextView tvname;
    @BindView(R.id.tvpname)
    TextView tvpname;
    @BindView(R.id.tvnum)
    TextView tvnum;
    @BindView(R.id.tvcasenum)
    TextView tvcasenum;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.logine)
    Button logine;
    @BindView(R.id.sws)
    Switch sws;
    @BindView(R.id.ivd)
    ImageView ivd;
    @BindView(R.id.tvres)
    TextView tvres;
    @BindView(R.id.tvs)
    TextView tvs;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tvwait)
    TextView tvwait;

    boolean flag = true;
    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;
    int scanfState = 0;
    CodeBeans codeBeans = new CodeBeans();
    String billID = "", billListID = "", productID = "";
    int caseBoxNum = 0, ynum = 0, snum = 0;
    List<String> codes = new ArrayList<>();
    Thread upThread = null;
    boolean exit = false;

    protected void initdata() {
        tvTitle.setText("生产关联扫描");
        tvRight.setVisibility(View.INVISIBLE);
        presenter.getDeatail(getIntent().getStringExtra(PDApp.billID));
        chooseDialog = new ChooseDialog(this, "是否开启删除条码", R.style.CommentDialog, new ChooseDialog.PriorityListener() {
            @Override
            public void setActivityText() {
                chooseDialog.dismiss();
                flag = false;
                ivd.setImageResource(R.mipmap.icon_check);
            }
        });

        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);
        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                        tvs.setText(dataa.getStr());
                        scanfState = dataa.getType();
                        if (scanfState == -1) {
                            tvs.setBackgroundResource(R.color.smalltext);
                        } else {
                            tvs.setBackgroundResource(R.color.green);
                        }
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        if (scanfState == -1) {
                            voiceUtils.voiceWarn();
                            tvres.setText("网络异常,请检查网络");
                            return;
                        }
                        if (flag) {
                            if (codes.size() < caseBoxNum ) {
                                if(!StringUtils.checkBoxCode(string)){
                                    voiceUtils.voiceWarn();
                                    tvres.setText("盒码长度不符");
                                    return;
                                }
                                if (snum >= ynum) {
                                    voiceUtils.voiceWarn();
                                    tvres.setText("实扫数已达到应扫数");
                                    return;
                                }
                                for (int i = 0; i < codes.size(); i++) {
                                    if (codes.get(i).equals(string)) {
                                        voiceUtils.voiceWarn();
                                        tvres.setText("盒码重复");
                                        return;
                                    }
                                }
                                codes.add(string);
                                tvcasenum.setText(codes.size() + "/" + caseBoxNum);
                                return;
                            } else if (codes.size() == caseBoxNum ) {
                                if(!StringUtils.checkCaseCode(string)){
                                    voiceUtils.voiceWarn();
                                    tvres.setText("箱码长度不符");
                                    return;
                                }
                                if (snum + caseBoxNum > ynum) {
                                    voiceUtils.voiceWarn();
                                    tvres.setText("实扫数已达到应扫数");
                                    return;
                                }
                                CodeBeans.Code cbean = new CodeBeans.Code();
                                cbean.setFlag(flag);
                                String strs=string;
                                for (int i = 0; i <codes.size() ; i++) {
                                    if(i==0){
                                        strs = string+";"+codes.get(i);
                                    }else {
                                        strs = strs+","+codes.get(i);
                                    }
                                }
                                cbean.setStr(strs);
                                codes.clear();
                                codeBeans.addCode(cbean);
                                tvwait.setText(codeBeans.getCodes().size()+"");
                            }
                        } else {
                            if (StringUtils.checkCaseCode(string)) {
                                if (snum < caseBoxNum) {
                                    voiceUtils.voiceWarn();
                                    tvres.setText("实扫数不满足删除条件");
                                    return;
                                }
                                CodeBeans.Code cbean = new CodeBeans.Code();
                                cbean.setFlag(flag);
                                cbean.setStr(string);
                                codeBeans.addCode(cbean);
                            } else if (StringUtils.checkBoxCode(string)) {
                                if (codes.contains(string)) {
                                    codes.remove(string);
                                    tvcasenum.setText(codes.size() + "/" + caseBoxNum);
                                    return;
                                } else {
                                    voiceUtils.voiceWarn();
                                    tvres.setText("当前扫描产品规格中不含此盒码");
                                    return;
                                }
                            } else {
                                voiceUtils.voiceWarn();
                                tvres.setText("数码长度不符");
                                return;
                            }
                        }
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        voiceUtils = new VoiceUtils(this);
        upThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!exit){
                    if(codeBeans.getCodes().size()>0){
                        if(codeBeans.getCodes().get(0).isFlag()){
                            presenter.addCode(billID, billListID, productID, codeBeans,codeBeans.getCodes().get(0).isFlagx());
                        }else{
                            presenter.deleteCode(billID, billListID, productID, codeBeans,codeBeans.getCodes().get(0).isFlagx());
                        }
                    }
                }
            }
        });
        upThread.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_scanf);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
        exit=true;
        upThread = null;
    }

    @OnClick({R.id.iv_title_left, R.id.logine, R.id.ivd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.ivd:
                if (flag) {
                    chooseDialog.show();
                } else {
                    flag = true;
                    ivd.setImageResource(R.mipmap.icon_nocheck);
                }
                break;
            case R.id.logine:
                String string = loginId.getText().toString();
                if (scanfState == -1) {
                    voiceUtils.voiceWarn();
                    tvres.setText("网络异常,请检查网络");
                    return;
                }
                if (flag) {
                    if (codes.size() < caseBoxNum ) {
                        if(!StringUtils.checkBoxCode(string)){
                            voiceUtils.voiceWarn();
                            tvres.setText("盒码长度不符");
                            return;
                        }
                        if (snum >= ynum) {
                            voiceUtils.voiceWarn();
                            tvres.setText("实扫数已达到应扫数");
                            return;
                        }
                        for (int i = 0; i < codes.size(); i++) {
                            if (codes.get(i).equals(string)) {
                                voiceUtils.voiceWarn();
                                tvres.setText("盒码重复");
                                return;
                            }
                        }
                        codes.add(string);
                        tvcasenum.setText(codes.size() + "/" + caseBoxNum);
                        return;
                    } else if (codes.size() == caseBoxNum ) {
                        if(!StringUtils.checkCaseCode(string)){
                            voiceUtils.voiceWarn();
                            tvres.setText("箱码长度不符");
                            return;
                        }
                        if (snum + caseBoxNum > ynum) {
                            voiceUtils.voiceWarn();
                            tvres.setText("实扫数已达到应扫数");
                            return;
                        }
                        CodeBeans.Code cbean = new CodeBeans.Code();
                        cbean.setFlag(flag);
                        String strs=string;
                        for (int i = 0; i <codes.size() ; i++) {
                            if(i==0){
                                strs = string+";"+codes.get(i);
                            }else {
                                strs = strs+","+codes.get(i);
                            }
                        }
                        cbean.setStr(strs);
                        codes.clear();
                        codeBeans.addCode(cbean);
                        tvwait.setText(codeBeans.getCodes().size()+"");
                    }
                } else {
                    if (StringUtils.checkCaseCode(string)) {
                        if (snum < caseBoxNum) {
                            voiceUtils.voiceWarn();
                            tvres.setText("实扫数不满足删除条件");
                            return;
                        }
                        CodeBeans.Code cbean = new CodeBeans.Code();
                        cbean.setFlag(flag);
                        cbean.setStr(string);
                        codeBeans.addCode(cbean);
                    } else if (StringUtils.checkBoxCode(string)) {
                        if (codes.contains(string)) {
                            codes.remove(string);
                            tvcasenum.setText(codes.size() + "/" + caseBoxNum);
                            return;
                        } else {
                            voiceUtils.voiceWarn();
                            tvres.setText("当前扫描产品规格中不含此盒码");
                            return;
                        }
                    } else {
                        voiceUtils.voiceWarn();
                        tvres.setText("数码长度不符");
                        return;
                    }
                }
                break;
        }
    }

    @Override
    public ProScanfModel createModel() {
        return new ProScanfModelImpl(this);
    }

    @Override
    public ProScanfView createView() {
        return this;
    }

    @Override
    public ProScanfPresent createPresenter() {
        return new ProScanfPresent();
    }

    @Override
    public void initView(List<ScanfBeanOut.DataBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                billID = list.get(0).getBillCode();
                billListID = list.get(0).getBillListID();
                productID = list.get(0).getProductID();
                for (int i = 0; i <PDApp.productBeanOut.getData().size() ; i++) {
                    if(PDApp.productBeanOut.getData().get(i).getId().equals(productID)){
                        caseBoxNum = PDApp.productBeanOut.getData().get(i).getCaseBoxNum();
                        break;
                    }
                }
                tvbill.setText(list.get(0).getBillCode());
                tvname.setText(list.get(0).getDesStoreName());
                tvpname.setText(list.get(0).getProductName());
                ynum = list.get(0).getyScanNums();
                snum = list.get(0).getsScanNums();
                tvnum.setText(snum + "/" + ynum);
                tvcasenum.setText(codes.size() + "/" + caseBoxNum);
                tvwait.setText(codeBeans.getCodes().size()+"");
            }
        });
    }

    @Override
    public void updateView(List<ScanfBeanOut.DataBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ynum = list.get(0).getyScanNums();
                snum = list.get(0).getsScanNums();
                tvnum.setText(snum + "/" + ynum);
                tvcasenum.setText(codes.size() + "/" + caseBoxNum);
                tvwait.setText(codeBeans.getCodes().size()+"");
                tvres.setText("数码处理完成");
            }
        });
    }

    @Override
    public void showres(String str) {
        tvnum.setText(snum + "/" + ynum);
        tvcasenum.setText(codes.size() + "/" + caseBoxNum);
        tvwait.setText(codeBeans.getCodes().size()+"");
        tvres.setText(str);
        voiceUtils.voiceWarn();
    }
}
